﻿#region Copyright (C) Sector7 Software Studio. All rights reserved
/*******************************************************************************************
 * Creation
 *      Author:         Wolf Lai
 *      Date:           2013/5/20 星期一 22:59:03
 *      Version:        
 * Modification 
 *      Author: 
 *      Date: 
 *      Description: 
 *      Version:
 *******************************************************************************************/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using Sector7.Medical.EntityType;
using Sector7.Framework.DB;

namespace Sector7.Medical.DAO.Standard
{
    /// <summary>
    /// 单位数据访问对象
    /// </summary>
    public class UnitDAO : DAOBase<M7_Unit>, IUnitDAO
    {
        #region Public functions

        public override object Create(M7_Unit unit)
        {
            string sql = @"
                INSERT INTO M7_Unit
                        (UnitName
                        ,Flags)
                    VALUES
                        (@UnitName
                        ,@Flags)
                SELECT @@IDENTITY";

            using (DBHelper helper = new DBHelper())
            {
                return helper.ExecuteScalar(sql,
                    DBUtil.CreateSqlParameter(unit.UnitName, "UnitName"),
                    DBUtil.CreateSqlParameter(unit.Flags, "Flags"));
            }
        }

        public override List<M7_Unit> ReadAll()
        {
            string sql = @"
                SELECT *
                    FROM M7_Unit";

            using (DBHelper helper = new DBHelper())
            {
                List<M7_Unit> units = new List<M7_Unit>();

                SqlDataReader reader = helper.ExecuteQuery(sql);
                while (reader.Read())
                {
                    var unit = new M7_Unit();
                    DBUtil.ReadData(reader, unit);

                    units.Add(unit);
                }

                return units;
            }
        }

        public List<M7_Unit> ReadAll(int flags)
        {
            string sql = @"
                SELECT *
                    FROM M7_Unit
                    WHERE Flags = @Flags";

            using (DBHelper helper = new DBHelper())
            {
                List<M7_Unit> units = new List<M7_Unit>();

                SqlDataReader reader = helper.ExecuteQuery(sql,
                    DBUtil.CreateSqlParameter(flags, "Flags"));
                while (reader.Read())
                {
                    object unit = new M7_Unit();
                    DBUtil.ReadData(reader, unit);
                    
                    units.Add(unit as M7_Unit);
                }

                return units;
            }
        }

        #endregion
    }
}
